# -*- coding: utf-8 -*-
"""
Created on Fri Oct 17 12:20:43 2025

@author: xiaok
"""
import mne
import os
import json
import mne
import matplotlib.pyplot as plt
import numpy as np

plt.close('all')
dataFolder = '..\\data\\'

fileFolderName = "10291712"
fileFolderName = "10291749"
fileFolderName = "11041021"

filePathName = dataFolder+fileFolderName+"\\"+fileFolderName+"_.bdf"

raw = mne.io.read_raw_bdf(filePathName,preload=True) # preload should be True, if you want to use filter like below 

nchannels, nsamples = raw.get_data().shape      

print(raw.ch_names)


# raw.notch_filter(freqs=50)
# raw.filter(l_freq=.3, h_freq=90,method='iir') # 5, 10, 20uv use this
# raw.filter(l_freq=.3,h_freq=None,method='iir') # 50, 100, 200uv use this

fs = raw.info['sfreq']
print('fs : '+str(fs))

# raw.plot(duration=5,n_channels=1,show=True)

# raw.plot_psd(fmin=1,fmax=100)

# data = raw.get_data()
# eeg_ch0 = data[0,:]

# eeg_ch0 = eeg_ch0*1000000 # make the unit to uV

# plt.figure()
# plt.plot(eeg_ch0)

# events_from_annot,event_dict = mne.events_from_annotations(raw)

# List of the events
# "trial_end":[13],             1
# "Cue_onset_left":[22],        2
# "Cue_onset_right":[23],       3
# "Cue_onset_up":[24],          4
# "Cue_onset_down":[25],        5
# "begin":[99],                 6
# fig = mne.viz.plot_events(events_from_annot,event_id=event_dict,sfreq=raw.info['sfreq'],first_samp=raw.first_samp)

# ch_names = raw.ch_names



#%%
with open('..\\task\\task_markers.json', 'r') as file:
    markers = json.load(file)
    
label_cue_left_int = markers['Cue_onset_left'][0]
label_cue_right_int = markers['Cue_onset_right'][0]
label_cue_up_int = markers['Cue_onset_up'][0]
label_cue_down_int = markers['Cue_onset_down'][0]    

events_from_annot,event_dict = mne.events_from_annotations(raw)
# List of the events
# "trial_end":[13],             1
# "Cue_onset_left":[22],        2
# "Cue_onset_right":[23],       3
# "Cue_onset_up":[24],          4
# "Cue_onset_down":[25],        5
# "begin":[99],                 6


events_from_annot,event_dict = mne.events_from_annotations(raw)
fig = mne.viz.plot_events(events_from_annot,event_id=event_dict,sfreq=raw.info['sfreq'],first_samp=raw.first_samp)


# event_id = [event_dict[str(label_cue_left_int)],
#             event_dict[str(label_cue_right_int)],
#             event_dict[str(label_cue_down_int)]]
# event_id = [
#             event_dict[str(label_cue_right_int)],
#             event_dict[str(label_cue_up_int)]
#             ]
event_id = [event_dict[str(label_cue_left_int)],
            event_dict[str(label_cue_right_int)],
            event_dict[str(label_cue_up_int)],
            event_dict[str(label_cue_down_int)]]


epochs = mne.Epochs(raw, events_from_annot, event_id=event_id, tmin=0.2, tmax=2.5, reject=None, baseline=None, preload=True)
data = epochs.get_data()



        
# event_dict_new = {value:int(float(key)) for key,value in event_dict.items()}
# events_from_annot_new = events_from_annot.copy()
# for i, c in enumerate(events_from_annot[:,2]):
#     events_from_annot_new[i,2]=event_dict_new[c]

# evnet_dict_marker = {
#     'left':label_cue_left_int,
#     'right':label_cue_right_int,
#     'up':label_cue_up_int,
#     'down':label_cue_down_int
# }

# signal_win_start = 0.5 #second
# signal_win_end = 2.5 #second

# epochs = mne.Epochs(
#     raw,
#     events_from_annot_new,
#     event_id=evnet_dict_marker,
#     tmin=signal_win_start,
#     tmax=signal_win_end,
#     baseline = None
# )

# data = epochs.get_data()
# labels = epochs.events[:,-1]



